package com.life.notes.config.security.service;

import com.life.notes.config.security.model.SysUserDetails;
import com.life.notes.model.SysUserEntity;
import com.life.notes.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * SpringSecurity用户的业务实现
 * 实现 UserDetailsService 接口里面的方法 实现自己的逻辑
 * @author lishuqian
 * @date 2020/6/16
 */
@Component
public class SysUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询用户信息
     * @author zwq
     * @date 2020/4/4
     * @param username
     * @return
     **/
    @Override
    public SysUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询用户信息
        SysUserEntity sysUserEntity =sysUserService.selectUserByName(username);
        if (sysUserEntity!=null){
            // 组装参数
            SysUserDetails sysUserDetails = new SysUserDetails();
            BeanUtils.copyProperties(sysUserEntity,sysUserDetails);
            return sysUserDetails;
        }
        return null;
    }
}
